<?php
namespace Api\Controller;
use Think\Controller;
class CarController extends CommonController {

    public function shop(){
        $map['shop_id'] = $_GET['shop_id'];
        $info = M("Shop")->where($map)->find();

        $info['pinpai'] = M("CarShopBrand")->where($map)->select();
        $info['member'] = M("CarShopMember")->where($map)->select();
        $img = M("CarShopImg")->where($map)->select();
        foreach ($img as $key => $value) {
            $img[$key]['pics'] = explode(',', $value['pics']);
        }
        $info['jiaoche'] = $img;

        $info['pics'] = explode(",", $info['pics']);
        $this->ajaxReturn($info);
    }

    public function brand(){
        $map['name'] = array('like',"%{$_GET['keyword']}%");
        $list = M("CarBrand")->where($map)->select();
        $this->ajaxReturn($list);
    }

    public function brandList(){
        $eq['openid'] = $_GET['openid'];
        $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        // $map['name'] = array('like',"%{$_GET['keyword']}%");
        $list = M("CarShopBrand")->where($map)->select();
        $this->ajaxReturn($list);
    }

    public function brandAdd(){
        $eq['openid'] = $_GET['openid'];
        $new['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        $eq['brand_id'] = $_GET['id'];
        if(M("CarShopBrand")->where($eq)->find()){
            $arr = array("err_code"=>0, "err_msg"=>"添加成功！");
            $this->ajaxReturn($arr);
        }

        $eq1['id'] = $_GET['id'];
        $data = M("CarBrand")->where($eq1)->find();
        $new['name'] = $data['name'];
        $new['brand_id'] = $data['brand_id'];
        $new['logo'] = $data['logo'];
        if(M("CarShopBrand")->add($new)){
            $arr = array("err_code"=>0, "err_msg"=>"添加成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }

    public function brandDel(){
        $eq['openid'] = $_GET['openid'];
        $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        $map['id'] = $_GET['id'];

        if(M("CarShopBrand")->where($map)->delete()){
            $arr = array("err_code"=>0, "err_msg"=>"删除成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }

    public function car(){
        echo $res = $this->ihttp_get("http://api.jisuapi.com/car/search?appkey=29aa532407c2946d&keyword={$_GET['keyword']}");
    }

    public function carList(){
        $map = array();
        $map['status'] = 1;
        if($_GET['openid']){
            $map['openid'] = $_GET['openid'];
        }
        if($_GET['shop_id']){
            $map['shop_id'] = $_GET['shop_id'];
        }
        $map['type'] = 'car';
        $list = M("Goods")->where($map)->order("last_time desc")->select();
        $this->ajaxReturn($list);
    }

    public function carAdd(){
        foreach ($_POST as $key => $value) {
            if(!is_numeric($value) && empty($value)){
                $arr = array("err_code"=>400,"err_msg"=>"请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        if($_POST['id']){
            $map['id'] = $_POST['id'];
            $map['openid'] = $_POST['openid'];
            if(M("Goods")->where($map)->save($_POST)){
                $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"没有进行任何修改哦！");
                $this->ajaxReturn($arr);
            }
        }else{
            $detail = S("cardetail{$_POST['car_id']}");

            $eq['openid'] = $_POST['openid'];
            $_POST['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
            $_POST['wid'] = session('wid');
            $_POST['old_price'] = $detail['price'];
            $_POST['create_time'] = date("Y-m-d H:i:s");
            $_POST['status'] = 1;
            $_POST['type'] = 'car';

            if(M("Goods")->add($_POST)){
                $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
                $this->ajaxReturn($arr);
            }
        }
    }

    public function member(){
        if($_GET['openid']){
            $eq['openid'] = $_GET['openid'];
            $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        }else{
            $map['shop_id'] = $_GET['shop_id'];
        }
        // $map['name'] = array('like',"%{$_GET['keyword']}%");
        $list = M("CarShopMember")->where($map)->select();
        $this->ajaxReturn($list);
    }

    public function memberItem(){
        $map['id'] = $_GET['id'];
        // $map['name'] = array('like',"%{$_GET['keyword']}%");
        $info = M("CarShopMember")->where($map)->find();
        $this->ajaxReturn($info);
    }

    public function memberAdd(){
        foreach ($_POST as $key => $value) {
            if(!is_numeric($value) && empty($value)){
                $arr = array("err_code"=>400,"err_msg"=>"请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        if($_POST['id']){
            $map['id'] = $_POST['id'];
            $map['openid'] = $_POST['openid'];
            if(M("CarShopMember")->where($map)->save($_POST)){
                $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"没有进行任何修改哦！");
                $this->ajaxReturn($arr);
            }
        }else{
            $eq['openid'] = $_POST['openid'];
            $_POST['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
            $_POST['wid'] = session('wid');

            if(M("CarShopMember")->add($_POST)){
                $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
                $this->ajaxReturn($arr);
            }
        }
    }

    public function memberDel(){
        $map['id'] = $_GET['id'];
        if(M("CarShopMember")->where($map)->delete()){
            $arr = array("err_code"=>0, "err_msg"=>"删除成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }

    public function img(){
        if($_GET['openid']){
            $eq['openid'] = $_GET['openid'];
            $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        }else{
            $map['shop_id'] = $_GET['shop_id'];
        }
        // $map['name'] = array('like',"%{$_GET['keyword']}%");
        $list = M("CarShopImg")->where($map)->select();
        foreach ($list as $key => $value) {
            $list[$key]['pics'] = explode(',', $value['pics']);
        }
        $this->ajaxReturn($list);
    }

    public function imgItem(){
        $map['id'] = $_GET['id'];
        // $map['name'] = array('like',"%{$_GET['keyword']}%");
        $info = M("CarShopImg")->where($map)->find();
        $info['pics'] = explode(",", $info['pics']);
        $this->ajaxReturn($info);
    }

    public function imgAdd(){
        foreach ($_POST as $key => $value) {
            if(!is_numeric($value) && empty($value)){
                $arr = array("err_code"=>400,"err_msg"=>"请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        if($_POST['id']){
            $map['id'] = $_POST['id'];
            $map['openid'] = $_POST['openid'];
            if(M("CarShopImg")->where($map)->save($_POST)){
                $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"没有进行任何修改哦！");
                $this->ajaxReturn($arr);
            }
        }else{
            $eq['openid'] = $_POST['openid'];
            $_POST['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
            $_POST['wid'] = session('wid');

            if(M("CarShopImg")->add($_POST)){
                $arr = array("err_code"=>0, "err_msg"=>"发布成功！");
                $this->ajaxReturn($arr);
            }else{
                $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
                $this->ajaxReturn($arr);
            }
        }
    }

    public function imgDel(){
        $map['id'] = $_GET['id'];
        if(M("CarShopImg")->where($map)->delete()){
            $arr = array("err_code"=>0, "err_msg"=>"删除成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }

    public function getAllCarBrand () {
//        $carBrand = M('car_brand');
//        $list = $carBrand->select();
//        foreach ($list as $key => $value) {
//            $row = M('goods')->where(['car_id'=>$value['brand_id'], 'wid' => session('wid')])->select();
//            if (!$row) {
//                unset($list[$key]);
//            }
//        }
        $where['a.shop_id'] = I("shop_id");
        if(I("appid")){
            $wechat = M("wechat")->where(['appid'=>I('appid')])->find();
            $where['a.shop_id'] = $wechat['shop_id'];
        }
        $list = M('car_shop_brand')->alias('a')
                                   ->field('a.id, b.brand_id, b.name, b.display, b.logo, b.letter')
                                   ->join('tp_car_brand b on a.brand_id = b.brand_id')
                                   ->where($where)
                                   ->order('letter')
                                   ->select();
        $this->ajaxReturn($list);
    }

    public function getTypeCar () {
        $typeid = I('typeid');
        $goods = M('goods');
        if ($typeid) {
            $list = $goods->where(['car_brand_id'=>$typeid, 'wid' => session('wid')])->select();
        } else {
            $list = $goods->where(['type' => 'car', 'wid' => session('wid')])->select();
        }
        $this->ajaxReturn($list);
    }

    public function getSortCar () {
        $goods = M('goods');
        $carId = I('typeid');
        $num   = I('num');
        $num?($num == 1?$order = 'price':$order = 'price desc'):$order = '';
        $carId?$where = ['car_brand_id'=>$carId]:$where = ['type' => 'car'];
        $where[] = ['wid' => session('wid')];
        $list  = $goods->where($where)->order($order)->select();
        $this->ajaxReturn($list);
    }

    public function verJionEndTime () {
        $shopId = I('shop_id');
        if(I("appid")){
            $wechat = M("wechat")->where(['appid'=>I('appid')])->find();
            $shopId = $wechat['shop_id'];
        }
        $list = M('shop')->where(['shop_id' => $shopId])->find();
        $list['join_end_time'] > date('Y-m-d H:i:s', time())?$this->ajaxReturn(1):$this->ajaxReturn(0);
    }
}